#coding:utf-8
from utils.captcha.captcha import create_captcha
from models.account.account_user_model import User
from datetime import datetime
from random import randint
import traceback
#from libs.yun_tong_xun.DEMO.SendTemplateSMS import sendTemplateSMS
from libs.dayu.sendSMS import sendTemplateSMS
from libs.common.send_mail.send_email_libs import send_qq_html_email
def create_captcha_img(self,pre_code,code):
    if pre_code:
        self.conn.delete('captcha:%s'%pre_code)
    text,img=create_captcha()
    self.conn.setex('captcha:%s'%code,text.lower(),60)
    return img

def is_logined(func):
    def wrapper(self,*args):
        if self.current_user is not None:
            return func(self,*args)
        else:
            return self.redirect('/auth/user_login')
    return wrapper



def auth_captcha(self,captcha_code,code):
    print captcha_code,code
    if captcha_code=='':
        return {'status':False,'msg':'请输入图形验证码!'}
    elif self.conn.get('captcha:%s'%code)!= captcha_code.lower():
        return {'status':False,'msg':'输入的图形验证码不正确!'}
    return {'status':True,'msg':'正确!'}

def login(self,name,password):
    print name,password
    if name=='' and password=='':
        return {'status':False,'msg':'请输入用户名或密码'}
    user=User.by_name(name)
    if user and user.auth_password(password):
        user.last_ogin=datetime.now()
        user.loginnum+=1
        self.db.add(user)
        self.db.commit()
        self.session.set('user_name',user.name)
        return {'status':True,'msg':'登陆成功!'}
    return {'status':False,'msg':'用户,密码错误'}

def logout(self):
    if self.current_user is not None:
        self.session.delete('user_name')

def get_mobile_code_lib(self,mobile,code,captcha):
    # if isinstance(mobile,unicode):
    #     mobile.encode('utf-8')
    if self.conn.get('captcha:%s'%code)!=captcha.lower():
        return {'status':False,'msg':u'图形验证码不正确'}
    mobile_code=randint(1000,9999)
    self.conn.setex('mobile_code:%s'%mobile,mobile_code,200)
    print mobile_code
    sendTemplateSMS(mobile,[mobile_code,30],1)
    return {'status':True,'msg':u'验证码已发送到%s,请查收'%mobile}

def regist(self,name,mobile,mobile_captcha,
                      password1,password2,captcha,code):
    '''验证邮箱验证码'''
    user=User.by_name(name)
    if user is not None:
        return {'status':False,'msg':'用户名已存在'}
    if self.conn.get('captcha:%s'%code)!=captcha.lower():
        return {'status':False,'msg':'图形验证码不正确'}
    if self.conn.get('mobile_code:%s'%mobile)!=mobile_captcha:
        return {'status':False,'msg':'短信验证不正确'}
    if password1!=password2:
        return {'status':False,'msg':'两次密码不正确'}

    user=User()
    user.name=name
    user.password=password1
    user.mobile=mobile
    self.db.add(user)
    self.db.commit()
    return {'status':True,'msg':'注册成功'}

def add_avatar_lib(self,avatar_data):
    '''上传用户头像'''
    try:
        user=self.current_user
        user.avatar=avatar_data
        user.updatetime=datetime.now()
        self.db.add(user)
        self.db.commit()
    except Exception as e:
        print e
        print '------------------'
        print traceback.format_exc()
        print '------------------'
        send_qq_html_email(
                '260760495@qq.com',
                ['260760495@qq.com',],
                '上传头像报错',
                traceback.format_exc().replace('\n','<br>'),
        )
        return {'status':False,'msg':'error'}
    return {'status':True,'msg':u'头像上传成功'}
